<?php

/**
 * This is the model class for table "tbl_ask".
 *
 * The followings are the available columns in table 'tbl_ask':
 * @property integer $ask_id
 * @property string $title
 * @property string $body
 * @property integer $user_id
 * @property integer $hits
 * @property integer $replyCount
 * @property integer $isClose
 * @property string $createdOn
 * @property integer $tags
 *
 * The followings are the available model relations:
 * @property TblUser $user
 * @property TblAskReply[] $tblAskReplies
 */
class Ask extends CActiveRecord
{
                    public $verifyCode;
                    public $page;
                    /**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return 'tbl_ask';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('title, body, verifyCode', 'required'),
                                                                array('title', 'unique'),
			array('user_id, hits, replyCount, isClose, tags', 'numerical', 'integerOnly'=>true),
			array('title', 'length', 'max'=>300),	
			// The following rule is used by search().
			// @todo Please remove those attributes that should not be searched.
			array('ask_id, title, body, user_id, hits, replyCount, isClose, createdOn, tags', 'safe', 'on'=>'search'),
                                                                array('verifyCode', 'captcha', 'allowEmpty'=>!CCaptcha::checkRequirements()),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
			'user' => array(self::BELONGS_TO, 'User', 'user_id'),
			'askReplies' => array(self::HAS_MANY, 'AskReply', 'ask_id', 'condition'=>'isFloor=1 AND status=1'),
                                                                'floorsCount' => array(self::STAT, 'AskReply', 'ask_id', 'condition'=>'isFloor=1 AND status=1'),
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'ask_id' =>  Yii::t('ask/model',  'Ask'),
			'title' =>Yii::t( 'ask/model', 'Title'),
			'body' => Yii::t('ask/model', 'Body'),
			'user_id' => Yii::t('ask/model', 'User'),
			'hits' => Yii::t('ask/model', 'Hits'),
			'replyCount' => Yii::t('ask/model',  'Reply Count'),
			'isClose' =>  Yii::t('ask/model', 'Is Close'),
			'createdOn' => Yii::t('ask/model',  'Created On'),
			'tags' => Yii::t('ask/model',  'Tags'),
                                                                'verifyCode'=>Yii::t('ask/model', 'Verify Code'),
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 *
	 * Typical usecase:
	 * - Initialize the model fields with values from filter form.
	 * - Execute this method to get CActiveDataProvider instance which will filter
	 * models according to data in model fields.
	 * - Pass data provider to CGridView, CListView or any similar widget.
	 *
	 * @return CActiveDataProvider the data provider that can return the models
	 * based on the search/filter conditions.
	 */
	public function search()
	{
		// @todo Please modify the following code to remove attributes that should not be searched.

		$criteria=new CDbCriteria;

		$criteria->compare('ask_id',$this->ask_id);
		$criteria->compare('title',$this->title,true);
		$criteria->compare('body',$this->body,true);
		$criteria->compare('user_id',$this->user_id);
		$criteria->compare('hits',$this->hits);
		$criteria->compare('replyCount',$this->replyCount);
		$criteria->compare('isClose',$this->isClose);
		$criteria->compare('createdOn',$this->createdOn,true);
		$criteria->compare('tags',$this->tags);

		return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
		));
	}

	/**
	 * Returns the static model of the specified AR class.
	 * Please note that you should have this exact method in all your CActiveRecord descendants!
	 * @param string $className active record class name.
	 * @return Ask the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}
        
                    /**
                    * 保存数据前的数据处理
                    * 
                    * @ author : Huang Yuetian
                    * @date : 07/23/2014
                    */
	public function beforeSave()
	{
		if ($this->isNewRecord)
		{
			$this->user_id = Yii::app()->user->id;
		}
		return parent::beforeSave();
	}
	
	/**
	 *获取ask的id并保存回复
	 *
	 *@author ： Huang Yuetian
	 *@date : 07/24/2014
	 */
	public function addAskReply($reply)
	{
		$reply->ask_id = $this->ask_id;
		return $reply->save();
	}
}
